Skip to content

feat(generator): update package.json template for monorepo compatibility#8694

Open
suztomo wants to merge 9 commits into
googleapis:mainfrom
suztomo:fix-generator-template
Open

feat(generator): update package.json template for monorepo compatibility#8694
suztomo wants to merge 9 commits into
googleapis:mainfrom
suztomo:fix-generator-template

Conversation

@suztomo

@suztomo suztomo commented Jun 22, 2026

Copy link
Copy Markdown
Member

This PR updates the package.json templates in gapic-generator-typescript to generate monorepo-compatible repository and homepage fields.

It introduces the monorepoDirectory getter to the API class (defaulting to naming.namePath) and uses it in the templates (as api.monorepoDirectory) to determine the repository directory and homepage.

Examples of resolved directories:

  • For google-cloud-kms (proto package google.cloud.kms.v1), it generates packages/google-cloud-kms (correct).
  • For google-maps-mapmanagement (proto package google.maps.mapmanagement.v2beta), it generates packages/google-maps-mapmanagement (correct).
  • For historical packages where the directory name doesn't match the proto package structure (like google-cloud-monitoring which has proto package google.monitoring.v3), it defaults to packages/google-monitoring (non-existent URL in baselines, but accepted for now to keep generator changes minimal).

Note

Generator Limitation: Because the generator runs as a protoc plugin, it only generates files with relative paths to the output root. It does not know the actual absolute path or the monorepo structure where these files will be written (that is handled by protoc or librarian which calls it). This is why it cannot dynamically resolve the correct monorepo directory name without complex lookup logic or external options.

This avoids manual fixes when onboarding new packages (like google-cloud-agentregistry) to this monorepo, and correctly handles packages where the npm scope name doesn't match the directory prefix (e.g., @google-cloud/mapmanagement -> packages/google-maps-mapmanagement).

Changes:

  • 'repository': points to the monorepo 'google-cloud-node.git' with the correct 'directory' field using api.naming.monorepoDirectory.
  • 'homepage': points to the package directory in the monorepo using api.naming.monorepoDirectory.
  • 'version': set default version to 0.0.0 in templates (updated from 0.1.0).
Why do we have two package.json templates?

The generator maintains two sets of templates under templates/cjs (CommonJS) and templates/esm (ESM).

  • CJS templates are used by default for the packages currently generated in this monorepo.
  • ESM templates were introduced to support dual-format (CJS/ESM) library generation (e.g., via the generator's format=esm flag).

Both CJS and ESM templates must be kept in sync so that future ESM-enabled libraries are generated with the correct monorepo metadata.

@suztomo suztomo requested a review from a team as a code owner June 22, 2026 14:35

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the CJS and ESM package.json templates to conditionally set the repository and homepage fields depending on whether the API publish name contains a slash. The reviewer noted that using whitespace-stripping tags (both {%- and -%}) on both sides of the control blocks will strip newlines and cause fields to concatenate onto the same line, resulting in poorly formatted JSON. They suggested only stripping the preceding newline to maintain proper formatting.

@suztomo suztomo force-pushed the fix-generator-template branch from 0a74544 to 36c0a43 Compare June 22, 2026 14:38
@suztomo suztomo force-pushed the fix-generator-template branch from 36c0a43 to 1dc7de5 Compare June 22, 2026 14:40
Comment thread core/generator/gapic-generator-typescript/typescript/src/schema/naming.ts Outdated
@shivanee-p shivanee-p requested review from danieljbruce and removed request for a team June 22, 2026 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant